package com.vinci.webtrading.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vinci.webtrading.dao.IClientDao;
import com.vinci.webtrading.dao.config.HibernateUtil;
import com.vinci.webtrading.dao.entity.Client;

public class ClientDaoImpl implements IClientDao {

	public void addClient(Client client) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.save(client);
			transaction.commit();

		} catch (Exception e) {
			System.out.println("ClientDaoImpl.saveClient()");
			System.out.println("Erreur d ajout de client: " + e);
			e.printStackTrace();

		} finally {
			session.close();
		}

	}

	public void updateClient(Client client) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			Client oldClient = (Client) session.get(Client.class,
					client.getIdClient());
			oldClient.setNom(client.getNom());
			oldClient.setPrenom(client.getPrenom());
			oldClient.setCin(client.getCin());
			oldClient.setNaissance(client.getNaissance());
			oldClient.setEmail(client.getEmail());
			oldClient.setAdresse(client.getAdresse());
			oldClient.setTel(client.getTel());
			oldClient.setLogin(client.getLogin());
			oldClient.setPassword(client.getPassword());
			transaction.commit();
		} catch (Exception e) {
			System.out.println("ClientDaoImpl.updateClient()"); // systrac
			System.out.println("Erreur de modification de client: " + e);
			e.printStackTrace();

		} finally {
			session.flush();
			session.close();
		}
	}

	public void deleteClient(Integer id) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			Client client = (Client) session.get(Client.class, id);
			session.delete(client);
			transaction.commit();
		} catch (Exception e) {
			System.out.println("ClientDaoImpl.deleteClient()");
			System.out.println("Erreur de suppression de client: " + e);
			e.printStackTrace();

		} finally {
			session.flush();
			session.close();
		}

	}

	public Client getClientById(Integer id) {
		Session session = null;
		Client client = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			client = (Client) session.get(Client.class, id);

		} catch (Exception e) {
			System.out.println("ClientDaoImpl.getClientById()");
			System.out.println("Erreur de recherche de client par Id: " + e);
			e.printStackTrace();

		} finally {
			session.flush();
			session.close();
		}
		return client;
	}

	public List<Client> getClientByKeyword(String keyword) {
		List<Client> client = new ArrayList<Client>();
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session
					.createQuery("from Client where nom like :pKeyword");
			query.setString("pKeyword", "%" + keyword + "%");
			client = query.list();

		} catch (Exception e) {
			System.out.println("ClientDaoImpl.getClientByKeyword()");
			System.out.println("Erreur de recherche de Client par mot cle: "
					+ e);
			e.printStackTrace();

		} finally {
			session.flush();
			session.close();
		}
		return client;
	}

	public List<Client> getAllClient() {
		Session session = null;
		List<Client> client = new ArrayList<Client>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.createQuery("from Client");
			client = query.list();

		} catch (Exception e) {
			System.out.println("ClientDaoImpl.getAllClient()");
			System.out.println("Erreur de recherche de tous les Client: " + e);
			e.printStackTrace();

		} finally {
			session.flush();
			session.close();
		}
		return client;

	}

}
